package cn.backpackerxl.easyJDBC.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:backpackerxl CreateTime:2022/4/17
 * FileName:BeanUtil
 **/
public final class BeanUtil {
    private BeanUtil() {
    }

    private static final Map<String, TableBean> objectMap = new HashMap<>();
    private static final Map<Class<?>, Object> objectPool = new HashMap<>();

    public static <E> E createBeanFactory(String packName) {
        return (E) createObj(packName);
    }

    public static Object createObj(String packName) {
        try {
            Object obj;
            if (objectMap.containsKey(packName)) {
                obj = objectMap.get(packName).getObject();
            } else {
                obj = Class.forName(packName).newInstance();
                TableBean tableBean = new TableBean();
                tableBean.setObject(obj);
                tableBean.setKey(SqlFactory.getTableId(obj));
                tableBean.setTableName(SqlFactory.getTableName(obj));
                objectMap.put(packName, tableBean);
            }
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static <T> T createTarget(String pack) {
        try {
            Class<?> clazz = Class.forName(pack);
            return (T) createObj(clazz);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Object createObj(Class<?> clazz) {
        Object o = objectPool.get(clazz);
        try {
            if (o == null) {
                o = clazz.newInstance();
                objectPool.put(clazz, o);
            }
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return o;
    }

    public static TableBean getBean(Object obj) {
        String packName = obj.getClass().getName();
        TableBean tableBean = objectMap.get(packName);
        if (tableBean == null) {
            TableBean t = new TableBean();
            t.setTableName(SqlFactory.getTableName(obj));
            t.setKey(SqlFactory.getTableId(obj));
            t.setObject(obj);
            objectMap.put(packName, t);
            return t;
        }
        return tableBean;
    }

    public static void setBean(Class<?> clazz) {
        try {
            Object obj = clazz.newInstance();
            TableBean tableBean = new TableBean();
            tableBean.setObject(obj);
            tableBean.setKey(SqlFactory.getTableId(obj));
            tableBean.setTableName(SqlFactory.getTableName(obj));
            objectMap.put(clazz.getName(), tableBean);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public static <E> E copyBean(E entity) {
        E o = null;
        try {
            o = (E) entity.getClass().newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return o;
    }
}
